package com.edu.glutdataconvertclient.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * @author ares
 */
@Slf4j
@SpringBootConfiguration
public class RabbitConfig {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Bean
    public AmqpTemplate amqpTemplate() {
        // 使用jackson 消息转换器
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.setEncoding("UTF-8");
        // 消息发送失败返回到队列中，yml需要配置 publisher-returns: true
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            String correlationId = message.getMessageProperties().getCorrelationId();
            log.debug("消息：{} 发送失败, 应答码：{} 原因：{} 交换机: {}  路由键: {}", correlationId, replyCode, replyText, exchange, routingKey);
        });
        // 消息确认，yml需要配置 publisher-confirms: true
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (ack) {
                log.debug("消息发送到exchange成功,id: {}", correlationData.getId());
            } else {
                log.debug("消息发送到exchange失败,原因: {}", cause);
            }
        });
        return rabbitTemplate;
    }

    @Bean("glutExchange")
    public Exchange directExchange() {
        return ExchangeBuilder.directExchange("glutExchange").durable(true).build();
    }

    @Bean("jhkczlbQUEUE")
    public Queue jhkczlbQUEUE() {
        return QueueBuilder.durable("jhkczlbQUEUE").build();
    }

    @Bean
    public Binding jhkczlbDirectBinding(@Qualifier("jhkczlbQUEUE") Queue queue,
                                        @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("jhkczlb").noargs();
    }

    @Bean("jsjbsjzlbQUEUE")
    public Queue jsjbsjzlbQUEUE() {
        return QueueBuilder.durable("jsjbsjzlbQUEUE").build();
    }

    @Bean
    public Binding jsjbsjzlbDirectBinding(@Qualifier("jsjbsjzlbQUEUE") Queue queue,
                                          @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("jsjbsjzlb").noargs();
    }

    @Bean("jxhbxxQUEUE")
    public Queue jxhbxxQUEUE() {
        return QueueBuilder.durable("jxhbxxQUEUE").build();
    }

    @Bean
    public Binding jxhbxxDirectBinding(@Qualifier("jxhbxxQUEUE") Queue queue,
                                       @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("jxhbxx").noargs();
    }

    @Bean("kcsjlbQUEUE")
    public Queue kcsjlbQUEUE() {
        return QueueBuilder.durable("kcsjlbQUEUE").build();
    }

    @Bean
    public Binding kcsjlbDirectBinding(@Qualifier("kcsjlbQUEUE") Queue queue,
                                       @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("kcsjlb").noargs();
    }

    @Bean("kcsjlbXxxsQUEUE")
    public Queue kcsjlbXxxsQUEUE() {
        return QueueBuilder.durable("kcsjlbXxxsQUEUE").build();
    }

    @Bean
    public Binding kcsjlbXxxsDirectBinding(@Qualifier("kcsjlbXxxsQUEUE") Queue queue,
                                           @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("kcsjlbXxxs").noargs();
    }

    @Bean("ksapsjlbQUEUE")
    public Queue ksapsjlbQUEUE() {
        return QueueBuilder.durable("ksapsjlbQUEUE").build();
    }

    @Bean
    public Binding ksapsjlbDirectBinding(@Qualifier("ksapsjlbQUEUE") Queue queue,
                                         @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ksapsjlb").noargs();
    }

    @Bean("skjsxxQUEUE")
    public Queue skjsxxQUEUE() {
        return QueueBuilder.durable("skjsxxQUEUE").build();
    }

    @Bean
    public Binding skjsxxDirectBinding(@Qualifier("skjsxxQUEUE") Queue queue,
                                       @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("skjsxx").noargs();
    }

    @Bean("xksjlbQUEUE")
    public Queue xksjlbQUEUE() {
        return QueueBuilder.durable("xksjlbQUEUE").build();
    }

    @Bean
    public Binding xksjlbDirectBinding(@Qualifier("xksjlbQUEUE") Queue queue,
                                       @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("xksjlb").noargs();
    }

    @Bean("ztjhzlbQUEUE")
    public Queue ztjhzlbQUEUE() {
        return QueueBuilder.durable("ztjhzlbQUEUE").build();
    }

    @Bean
    public Binding ztjhzlbDirectBinding(@Qualifier("ztjhzlbQUEUE") Queue queue,
                                        @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ztjhzlb").noargs();
    }

    @Bean("zyxxsjlbQUEUE")
    public Queue zyxxsjlbQUEUE() {
        return QueueBuilder.durable("zyxxsjlbQUEUE").build();
    }

    @Bean
    public Binding zyxxsjlbDirectBinding(@Qualifier("zyxxsjlbQUEUE") Queue queue,
                                         @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("zyxxsjlb").noargs();
    }

    @Bean("xjjbsjzlbQUEUE")
    public Queue xjjbsjzlbQUEUE() {
        return QueueBuilder.durable("xjjbsjzlbQUEUE").build();
    }

    @Bean
    public Binding xjjbsjzlbDirectBinding(@Qualifier("xjjbsjzlbQUEUE") Queue queue,
                                          @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("xjjbsjzlb").noargs();
    }

    @Bean("xsjbsjzlQUEUE")
    public Queue xsjbsjzlQUEUE() {
        return QueueBuilder.durable("xsjbsjzlQUEUE").build();
    }

    @Bean
    public Binding xsjbsjzlDirectBinding(@Qualifier("xsjbsjzlQUEUE") Queue queue,
                                         @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("xsjbsjzl").noargs();
    }

    @Bean("bjsjlbQUEUE")
    public Queue bjsjlbQUEUE() {
        return QueueBuilder.durable("bjsjlbQUEUE").build();
    }

    @Bean
    public Binding bjsjlbDirectBinding(@Qualifier("bjsjlbQUEUE") Queue queue,
                                       @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("bjsjlb").noargs();
    }

    @Bean("ksJiankaoQUEUE")
    public Queue ksJiankaoQUEUE() {
        return QueueBuilder.durable("ksJiankaoQUEUE").build();
    }

    @Bean
    public Binding ksJiankaoDirectBinding(@Qualifier("ksJiankaoQUEUE") Queue queue,
                                          @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ksJiankao").noargs();
    }

    @Bean("ksKsbjQUEUE")
    public Queue ksKsbjQUEUE() {
        return QueueBuilder.durable("ksKsbjQUEUE").build();
    }

    @Bean
    public Binding ksKsbjDirectBinding(@Qualifier("ksKsbjQUEUE") Queue queue,
                                       @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ksKsbj").noargs();
    }

    @Bean("ksKsQUEUE")
    public Queue ksKsQUEUE() {
        return QueueBuilder.durable("ksKsQUEUE").build();
    }

    @Bean
    public Binding ksKsDirectBinding(@Qualifier("ksKsQUEUE") Queue queue,
                                     @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ksKs").noargs();
    }

    @Bean("ksZyjsBjQUEUE")
    public Queue ksZyjsBjQUEUE() {
        return QueueBuilder.durable("ksZyjsBjQUEUE").build();
    }

    @Bean
    public Binding ksZyjsBjDirectBinding(@Qualifier("ksZyjsBjQUEUE") Queue queue,
                                         @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ksZyjsBj").noargs();
    }

    @Bean("ksZyjsKaoshengQUEUE")
    public Queue ksZyjsKaoshengQUEUE() {
        return QueueBuilder.durable("ksZyjsKaoshengQUEUE").build();
    }

    @Bean
    public Binding ksZyjsKaoshengDirectBinding(@Qualifier("ksZyjsKaoshengQUEUE") Queue queue,
                                               @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ksZyjsKaosheng").noargs();
    }

    @Bean("ksZyjsQUEUE")
    public Queue ksZyjsQUEUE() {
        return QueueBuilder.durable("ksZyjsQUEUE").build();
    }

    @Bean
    public Binding ksZyjsDirectBinding(@Qualifier("ksZyjsQUEUE") Queue queue,
                                       @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ksZyjs").noargs();
    }

    @Bean("bmxxQUEUE")
    public Queue bmxxQUEUE() {
        return QueueBuilder.durable("bmxxQUEUE").build();
    }

    @Bean
    public Binding bmxxDirectBinding(@Qualifier("bmxxQUEUE") Queue queue,
                                     @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("bmxx").noargs();
    }

    @Bean("jssysjzlbQUEUE")
    public Queue jssysjzlbQUEUE() {
        return QueueBuilder.durable("jssysjzlbQUEUE").build();
    }

    @Bean
    public Binding jssysjzlbDirectBinding(@Qualifier("jssysjzlbQUEUE") Queue queue,
                                          @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("jssysjzlb").noargs();
    }

    @Bean("jwKsqlcJgxxQUEUE")
    public Queue jwKsqlcJgxxQUEUE() {
        return QueueBuilder.durable("jwKsqlcJgxxQUEUE").build();
    }

    @Bean
    public Binding jwKsqlcJgxxDirectBinding(@Qualifier("jwKsqlcJgxxQUEUE") Queue queue,
                                            @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("jwKsqlcJgxx").noargs();
    }

    @Bean("viewKcbJsQUEUE")
    public Queue viewKcbJsQUEUE() {
        return QueueBuilder.durable("viewKcbJsQUEUE").build();
    }

    @Bean
    public Binding viewKcbJsDirectBinding(@Qualifier("viewKcbJsQUEUE") Queue queue,
                                          @Qualifier("glutExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("viewKcbJs").noargs();
    }
}
